package com.google.android.apps.iosched.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Parcelable;
import android.preference.PreferenceManager;

import com.google.android.apps.iosched.provider.ScheduleContract;

/**
 * Created by Alex on 13-12-5.
 */
public class BeamUtils {

    private static final byte[] DEFAULT_BEAM_MIME = "application/vnd.google.iosched.default".getBytes();

    private static final String PREE_BEAM_STATE = "beam_state";
    private static final int BEAM_STAT_UNLOCKED = 0x1481;
    private static final int BEAM_STAT_LOCKED = 0x0;


    public static boolean isBeamUnlocked(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return (prefs.getInt(PREE_BEAM_STATE,BEAM_STAT_LOCKED) == BEAM_STAT_UNLOCKED);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean wasLauchedThroughBeamFirstTime(Context context, Intent intent) {
        return intent != null
                && UIUtils.hasICS()
                && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
                && !isBeamUnlocked(context);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void setBeamSessionUrl(Activity activity,Uri sessionUri){
        if(UIUtils.hasICS()){
            NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
            if(nfcAdapter == null){
                return ;
            }
            nfcAdapter.setNdefPushMessage(
                    new NdefMessage(
                        new NdefRecord[]{
                                new NdefRecord(NdefRecord.TNF_MIME_MEDIA,ScheduleContract.Sessions.CONTENT_ITEM_TYPE.getBytes(),
                                        new byte[0],sessionUri.toString().getBytes())}),activity);
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void setDefaultBeamUri(Activity activity){
        if (UIUtils.hasICS()){
            NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
            if (nfcAdapter == null){
                return;
            }
            nfcAdapter.setNdefPushMessage(
                    new NdefMessage(
                            new NdefRecord[]{
                              new NdefRecord(
                                      NdefRecord.TNF_MIME_MEDIA,DEFAULT_BEAM_MIME,new byte[0],new byte[0])}
                    ),activity);
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void setBeamUnlocked(Context context){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putInt(PREE_BEAM_STATE,BEAM_STAT_UNLOCKED).commit();
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void tryUpdateIntentFromBeam(Activity activity){
        if (UIUtils.hasICS()){
            Intent originalIntent = activity.getIntent();
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())){
                Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                NdefMessage msg = (NdefMessage) rawMsgs[0];

                NdefRecord mimeRecord = msg.getRecords()[0];
                if (ScheduleContract.Sessions.CONTENT_ITEM_TYPE.equals(new String(mimeRecord.getType()))){
                    Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(new String(mimeRecord.getPayload())));
                    activity.setIntent(sessionDetailIntent);
                }
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void setBeamCompleteCallback(Activity activity,NfcAdapter.OnNdefPushCompleteCallback callback){
        if (UIUtils.hasICS()){
            NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
            if (adapter == null){
                return;
            }
            adapter.setOnNdefPushCompleteCallback(callback,activity);
        }
    }

    public static void launchBeamSession(Context context){
        context.startActivity(new Intent(Intent.ACTION_VIEW, ScheduleContract.Sessions.buildSearchUri("gooio2012/125/")));
    }

}
